package cn.wei.youvie.controller;


import cn.wei.youvie.common.annotation.AuthCheck;
import cn.wei.youvie.common.constant.UserConstant;
import cn.wei.youvie.common.exception.BusinessException;
import cn.wei.youvie.common.servlet.BaseResponse;
import cn.wei.youvie.common.servlet.DeleteRequest;
import cn.wei.youvie.common.servlet.ErrorCode;
import cn.wei.youvie.common.servlet.ResultUtils;
import cn.wei.youvie.domain.dto.menu.MenuAddRequest;
import cn.wei.youvie.domain.dto.menu.MenuQueryRequest;
import cn.wei.youvie.domain.dto.menu.MenuUpdateRequest;
import cn.wei.youvie.domain.vo.MenuVO;
import cn.wei.youvie.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Integer> add(@RequestBody MenuAddRequest menuAddRequest) {
        return ResultUtils.success(menuService.addMenu(menuAddRequest));
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateById(@RequestBody MenuUpdateRequest menuUpdateRequest) {
        if (menuUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(menuService.update(menuUpdateRequest));
    }

    @DeleteMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteById(@RequestBody DeleteRequest deleteRequest) {
        return ResultUtils.success(menuService.removeById(deleteRequest.getId()));
    }

    @PostMapping("/query")
    public BaseResponse<List<MenuVO>> getMenus(@RequestBody MenuQueryRequest menuQueryRequest) {
        if (menuQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(menuService.getMenus(menuQueryRequest));
    }

    @GetMapping("/all")
    public BaseResponse<List<MenuVO>> getMenus(HttpServletRequest request) {
        return ResultUtils.success(menuService.getAllMenus(request));
    }


}
